Explain Inline vs. External Templates in knockout.js.
Explain Inline vs. External Templates in knockout.js.
169
26-Jun-2024
Updated on 28-Jun-2024
Ashutosh Kumar Verma
28-Jun-2024Knockout.js Inline vs External Templates
Let's see the explanation of Inline template vs External template in knockout.js,
Inline Template
Inline templates in Knockout.js are defined directly in your HTML file, usually using a <script> tag with a specific attribute (text/html or text/x-knockout-template). These templates are built into HTML markup and can be referenced by Knockout.js bindings.
Example-
Explanation
Advantages
Simplicity- Inline templates are easy to define and maintain directly in an HTML file.
Readability- Dimensions appear next to the rest of the HTML markup, making it clear how they are used.
Also, Read: What are custom bindings in Knockout.js?
External Templates
In Knockout.js, external templates are stored in files and dynamically loaded into the application. They are either generated via AJAX or embedded as part of the application process. This approach separates HTML markup from JavaScript logic, encouraging better design and maintenance.
Example-
productTemplate.html
Index.html
Output-
Also, Read: Conditional html heading tag based on variable in knockout.js